# Maintainer: Bartłomiej Piotrowski <bpiotrowski@archlinux.org>
# Maintainer: Florian Pritz <bluewind@xinu.at>

pkgbase=zabbix
pkgname=(zabbix-server zabbix-agent zabbix-proxy zabbix-frontend-php)
pkgver=4.0.0
pkgrel=2
arch=(x86_64)
url='https://www.zabbix.com/'
license=(GPL)
makedepends=(postgresql-libs libxml2 unixodbc net-snmp libmariadbclient libldap libevent)
source=(https://downloads.sourceforge.net/sourceforge/zabbix/zabbix-${pkgver}.tar.gz
        patch_set_max_version_tls12_for_30.patch
		remove-tests.patch
        zabbix-agent.{service,sysusers,tmpfiles}
	zabbix-server{-mysql,-pgsql}.service zabbix-server.{sysusers,tmpfiles}
	zabbix-proxy{-sqlite,-mysql,-pgsql}.service zabbix-proxy.{sysusers,tmpfiles})

sha256sums=('5673e106156102aff8c6781a890da6cedfc975cf13d96d8749b4c7126f426bc7'
            '8e419efc3848b88bf9f05f957c8096af6f5ceb98e06253b1ca9c6993434b9b38'
            '5b3867907a8b6c3dda57f0658ced184273fa099aef7fcfc3efd312218e5b280f'
            '484fa9969eab61eaf20043ae08e2615c0569982dff869c985f2e2065da698c6a'
            'ef23133aae2340945e621c9725094a3458d9089d3de15f641afcdabdf7c5a39c'
            'c38a871d237b00dd6310dfb02e5a8e1b930e445204e73dde8305bed8baf380ec'
            'bac2f6d7b97e21ddc49f4d845e2810cda1854e07b208e7a097365a6eebc6093b'
            'f34a718ea7ff8f6f0e73299db708879d52d508e297a8b30c082ad2ff6e6d6c34'
            '9fe22526327a0573652c65ff374eba7c36a00b85f8e72d32643d1cc68a07ebbb'
            '879a7553040d652d1c34044c0ce1e57b809c647b3b037247a5819892a7622a65'
            'e1af49cdcfa992d2eb21ec5eb08dd907e1a8b6f88ccbe6a168b762c76fca8430'
            '073797c56eb1515e993591333c214aac05049c4bb57c618e1e2518fba23dd3b8'
            'da6e8f7ce2686e95d269569040835fb341ffe6db17296be31d9a75478443fab2'
            'd8929b2e78b5b178c7eebc32ae00a05b7c9d798cf25950a8745ea4b23734e226'
            'f842ea0a229513e918ff9641391605d436b7d57cb53d932cdd02e4e8b13c95d9')

prepare() {
  cd $pkgbase-$pkgver
  sed -i \
    -e '/^LogFile=.*/d' \
    -e 's/# LogType=file/LogType=system/' \
    conf/zabbix_{agentd,proxy,server}.conf

  # Temporary fix for compatibility with openssl 1.1.1 https://support.zabbix.com/browse/ZBX-14856
  patch -p0 -i "$srcdir/patch_set_max_version_tls12_for_30.patch"

  # remove tests from configure.ac because that part makes automake fail
  patch -p1 -i "$srcdir/remove-tests.patch"

  autoreconf -fi
}

build() {
  _configure_flags=(
    --disable-static
    --prefix=/usr
    --infodir=/usr/share/info
    --mandir=/usr/share/man
    --sysconfdir=/etc/zabbix
    --enable-agent
    --enable-ipv6
    --with-ldap
    --with-libcurl
    --with-libxml2
    --with-net-snmp
    --with-openssl
    --with-ssh2
    --with-unixodbc
  )

  cd $pkgbase-$pkgver

  for db in postgresql mysql; do
    ./configure ${_configure_flags[@]} --enable-server --with-$db
    make clean
    make
    mv src/zabbix_server/zabbix_server{,_$db}
  done

  for db in postgresql mysql sqlite3; do
    ./configure ${_configure_flags[@]} --enable-proxy --with-$db
    make clean
    make
    mv src/zabbix_proxy/zabbix_proxy{,_$db}
  done
}

package_zabbix-server() {
  pkgdesc='Monitoring software for networks and applications'
  depends=(net-snmp curl libxml2 unixodbc libldap libevent pcre)
  optdepends=('postgresql-libs: for PostgreSQL support'
              'libmariadbclient: for MariaDB support')
  backup=(etc/zabbix/zabbix_server.conf)
  install=zabbix-server.install

  cd $pkgbase-$pkgver

  for db in postgresql mysql; do
    install -Dm755 src/zabbix_server/zabbix_server_$db \
      "$pkgdir/usr/bin/zabbix_server_$db"

    install -d "$pkgdir/usr/share/$pkgname/$db"
    install -m644 database/$db/*.sql -t "$pkgdir/usr/share/$pkgname/$db"
  done
  install -Dm755 src/zabbix_get/zabbix_get "$pkgdir/usr/bin/zabbix_get"

  install -Dm644 man/zabbix_server.man "$pkgdir/usr/share/man/man8/zabbix_server.8"
  install -Dm644 man/zabbix_get.man "$pkgdir/usr/share/man/man1/zabbix_get.1"

  install -Dm644 conf/zabbix_server.conf "$pkgdir/etc/zabbix/zabbix_server.conf"
  chown 170:170 "$pkgdir/etc/zabbix/zabbix_server.conf"

  install -Dm644 "$srcdir/zabbix-server-pgsql.service" \
    "$pkgdir/usr/lib/systemd/system/zabbix-server-pgsql.service"
  install -Dm644 "$srcdir/zabbix-server-mysql.service" \
    "$pkgdir/usr/lib/systemd/system/zabbix-server-mysql.service"

  install -Dm644 "$srcdir/zabbix-server.sysusers" \
	"$pkgdir/usr/lib/sysusers.d/zabbix-server.conf"
  install -Dm644 "$srcdir/zabbix-server.tmpfiles" \
	"$pkgdir/usr/lib/tmpfiles.d/zabbix-server.conf"
}

package_zabbix-agent() {
  pkgdesc='Monitoring agent for Zabbix'
  depends=(curl pcre)
  backup=(etc/zabbix/zabbix_agentd.conf)

  cd $pkgbase-$pkgver
  install -Dm755 src/zabbix_agent/zabbix_agentd "$pkgdir/usr/bin/zabbix_agentd"
  install -Dm755 src/zabbix_sender/zabbix_sender "$pkgdir/usr/bin/zabbix_sender"

  install -Dm644 conf/zabbix_agentd.conf "$pkgdir/etc/zabbix/zabbix_agentd.conf"
  chown 172:172 "$pkgdir/etc/zabbix/zabbix_agentd.conf"

  install -Dm644 conf/zabbix_agentd/userparameter_examples.conf \
	"$pkgdir/usr/share/zabbix-agent/userparameter_examples.conf"
  install -Dm644 conf/zabbix_agentd/userparameter_mysql.conf \
	"$pkgdir/usr/share/zabbix-agent/userparameter_mysql.conf"

  install -Dm644 man/zabbix_agentd.man \
	"$pkgdir/usr/share/man/man8/zabbix_agentd.8"
  install -Dm644 man/zabbix_sender.man \
	"$pkgdir/usr/share/man/man1/zabbix_sender.1"

  install -Dm644 "$srcdir/zabbix-agent.service" \
	"$pkgdir/usr/lib/systemd/system/zabbix-agent.service"
  install -Dm644 "$srcdir/zabbix-agent.sysusers" \
	"$pkgdir/usr/lib/sysusers.d/zabbix-agent.conf"
  install -Dm644 "$srcdir/zabbix-agent.tmpfiles" \
	"$pkgdir/usr/lib/tmpfiles.d/zabbix-agent.conf"
}

package_zabbix-proxy() {
  pkgdesc='Data collecting proxy for Zabbix'
  depends=(net-snmp curl libxml2 sqlite unixodbc libldap pcre)
  optdepends=('libmariadbclient: for MariaDB support'
              'postgresql-libs: for PostgreSQL support')
  backup=(etc/zabbix/zabbix_proxy.conf)

  cd $pkgbase-$pkgver
  for db in postgresql mysql sqlite3; do
    install -Dm755 src/zabbix_proxy/zabbix_proxy_$db \
      "$pkgdir/usr/bin/zabbix_proxy_$db"

    install -d "$pkgdir/usr/share/$pkgname/$db"
    install -m644 database/$db/schema.sql -t "$pkgdir/usr/share/$pkgname/$db"
  done
  install -Dm644 conf/zabbix_proxy.conf "$pkgdir/etc/zabbix/zabbix_proxy.conf"
  chown 171:171 "$pkgdir/etc/zabbix/zabbix_proxy.conf"

  install -Dm644 "$srcdir/zabbix-proxy-sqlite.service" \
    "$pkgdir/usr/lib/systemd/system/zabbix-proxy-sqlite.service"
  install -Dm644 "$srcdir/zabbix-proxy-pgsql.service" \
    "$pkgdir/usr/lib/systemd/system/zabbix-proxy-pgsql.service"
  install -Dm644 "$srcdir/zabbix-proxy-mysql.service" \
    "$pkgdir/usr/lib/systemd/system/zabbix-proxy-mysql.service"

  install -Dm644 "$srcdir/zabbix-proxy.sysusers" \
	"$pkgdir/usr/lib/sysusers.d/zabbix-proxy.conf"
  install -Dm644 "$srcdir/zabbix-proxy.tmpfiles" \
	"$pkgdir/usr/lib/tmpfiles.d/zabbix-proxy.conf"
}

package_zabbix-frontend-php() {
  pkgdesc='PHP frontend for Zabbix'
  depends=(zabbix-server php php-gd)

  cd $pkgbase-$pkgver
  install -d "$pkgdir/usr/share/webapps/zabbix"
  cp -a frontends/php/* "$pkgdir/usr/share/webapps/zabbix"
  chown http:http ${pkgdir}/usr/share/webapps/zabbix/conf/ # write access for http user
}
